import { requestClient } from '#/api/request';

/**
 * 用户会员查询参数
 */
export interface UserMembershipListParams {
  pageNo?: number;
  pageSize?: number;
  userId?: number;
  levelId?: number;
  status?: number;
  autoRenew?: number;
  sortField?: string;
  isAsc?: number;
}

/**
 * 用户会员数据
 */
export interface UserMembership {
  id: number;
  userId: number;
  nickName: string;
  levelId: number;
  levelName: string;
  levelCode: string;
  startDate: string;
  endDate: string;
  status: number;
  statusText: string;
  autoRenew: number;
  autoRenewText: string;
  remainingDays: number;
  createdTime: string;
  updateTime: string;
}

/**
 * 用户会员表单数据
 */
export interface UserMembershipFormData {
  id?: number;
  userId: number;
  levelId: number;
  startDate: string;
  endDate: string;
  status: number;
  autoRenew: number;
}

/**
 * 续费参数
 */
export interface RenewParams {
  id: number;
  periodType: number;
  periodValue: number;
}

/**
 * 用户会员列表响应数据
 */
export interface UserMembershipListResult {
  pageNo: number;
  pageSize: number;
  result: UserMembership[];
  total: number;
  totalPages: number;
  start: number;
  end: number;
}

/**
 * API响应格式
 */
export interface ApiResponse<T> {
  code: number;
  message: string;
  data: T;
}

/**
 * 状态枚举
 */
export const STATUS_OPTIONS = [
  { value: 1, label: '有效' },
  { value: 0, label: '无效' },
];

/**
 * 自动续费枚举
 */
export const AUTO_RENEW_OPTIONS = [
  { value: 1, label: '是' },
  { value: 0, label: '否' },
];

/**
 * 周期类型枚举
 */
export const PERIOD_TYPE_OPTIONS = [
  { value: 1, label: '天卡' },
  { value: 2, label: '周卡' },
  { value: 3, label: '月卡' },
  { value: 4, label: '季卡' },
  { value: 5, label: '年卡' },
];

/**
 * 获取用户会员列表（分页）
 */
export async function getUserMembershipListApi(params?: UserMembershipListParams) {
  return requestClient.get<ApiResponse<UserMembershipListResult>>('/user-membership/list', { params });
}

/**
 * 获取用户会员详情
 */
export async function getUserMembershipDetailApi(id: number) {
  return requestClient.get<ApiResponse<UserMembership>>(`/user-membership/${id}`);
}

/**
 * 获取用户当前有效的会员信息
 */
export async function getActiveUserMembershipApi(userId: number) {
  return requestClient.get<ApiResponse<UserMembership>>(`/user-membership/active/${userId}`);
}

/**
 * 新增用户会员
 */
export async function createUserMembershipApi(data: UserMembershipFormData) {
  return requestClient.post<ApiResponse<UserMembership>>('/user-membership', data);
}

/**
 * 更新用户会员
 */
export async function updateUserMembershipApi(data: UserMembershipFormData) {
  return requestClient.put<ApiResponse<UserMembership>>('/user-membership', data);
}

/**
 * 取消用户会员
 */
export async function cancelUserMembershipApi(id: number) {
  return requestClient.put<ApiResponse<boolean>>(`/user-membership/cancel/${id}`);
}

/**
 * 续费用户会员
 */
export async function renewUserMembershipApi(params: RenewParams) {
  return requestClient.put<ApiResponse<UserMembership>>('/user-membership/renew', null, { params });
}

/**
 * 删除用户会员（支持批量）
 */
export async function deleteUserMembershipsApi(ids: number[]) {
  return requestClient.delete<ApiResponse<string>>('/user-membership', { data: ids });
}

/**
 * 处理过期会员（系统定时任务）
 */
export async function processExpiredMembershipsApi() {
  return requestClient.post<ApiResponse<number>>('/user-membership/process-expired');
}

/**
 * 发送会员到期提醒（系统定时任务）
 */
export async function sendMembershipRemindersApi(days: number) {
  return requestClient.post<ApiResponse<number>>(`/user-membership/send-reminders/${days}`);
}

/**
 * 获取状态文本
 */
export function getStatusText(status: number): string {
  const option = STATUS_OPTIONS.find(item => item.value === status);
  return option ? option.label : '未知';
}

/**
 * 获取自动续费文本
 */
export function getAutoRenewText(autoRenew: number): string {
  const option = AUTO_RENEW_OPTIONS.find(item => item.value === autoRenew);
  return option ? option.label : '未知';
}

/**
 * 获取周期类型文本
 */
export function getPeriodTypeText(periodType: number): string {
  const option = PERIOD_TYPE_OPTIONS.find(item => item.value === periodType);
  return option ? option.label : '未知';
}

/**
 * 计算剩余天数
 */
export function calculateRemainingDays(endDate: string): number {
  const end = new Date(endDate);
  const now = new Date();
  const diffTime = end.getTime() - now.getTime();
  const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
  return Math.max(0, diffDays);
}

/**
 * 格式化日期时间
 */
export function formatDateTime(dateTime: string): string {
  if (!dateTime) return '-';
  return new Date(dateTime).toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  });
} 
